package cn.lj.org.controller;

import cn.lj.base.util.AjaxResult;
import cn.lj.base.util.PageList;
import cn.lj.org.domain.Department;
import cn.lj.org.query.DepartmentQuery;
import cn.lj.org.service.IDepartmentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController //@Controller+@ResponseBody(转换json)
@RequestMapping("/dept")
@Api(tags = "部门接口",description = "部门接口详细描述")
public class DepartmentController {
    /*注入service对象*/
    @Autowired
    private IDepartmentService departmentService;
    /*根据ID查询数据*/
    @GetMapping("/{id}")
    @ApiOperation(value = "部门查找",notes = "根据ID查找")
    public Department findById(@PathVariable("id") Long id){
        Department department = departmentService.getById(id);
        return department;
    }
    @GetMapping
    @ApiOperation(value = "部门查找",notes = "查找所有部门")
    public List<Department> findAll(){
        List<Department> list = departmentService.getAll();
        return list;
    }
    @GetMapping("/tree")
    @ApiOperation(value = "部门查找",notes = "查找所有部门及其父类部门")
    public List<Department> findTree(){
       // System.out.println("------------------sssssssssssssssssssssss");
        List<Department> list = departmentService.getTree();
        return list;
    }
    /*高级查询，post请求，@RequestBody：请求的json数据转化为对象数据*/
    @PostMapping
    @ApiOperation(value = "部门高级查询",notes = "部门高级查询")
    public PageList<Department> findQuery(@RequestBody DepartmentQuery query){
        PageList<Department> data = departmentService.queryData(query);
        return data;
    }
    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除部门",notes = "根据ID删除部门")
    public AjaxResult del(@PathVariable("id")Long id){
        try {
            departmentService.del(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setAjaxResult(e.getMessage());
        }
    }
    @PatchMapping
    @ApiOperation(value = "删除部门",notes = "批量删除")
    public AjaxResult patchdel(@RequestBody List<Long> ids){
        try {
            System.out.println(ids);
            departmentService.patchdel(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setAjaxResult(e.getMessage());
        }
    }
    @PutMapping//添加或者修改
    @ApiOperation(value = "部门的添加或者修改",notes = "部门的添加或者修改")
    public AjaxResult updateOrSave(@RequestBody Department department){
        try {
            if(department.getId() == null){
                departmentService.add(department);
            }else {
                departmentService.upadte(department);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setAjaxResult(e.getMessage());
        }
    }
}
